import 'dart:async';

class MyTimer {
  Timer? _timer;
  int _seconds = 0;
  bool _isRunning = false;

  // 获取当前时间字符串
  String get timeString {
    int hours = _seconds ~/ 3600;
    int minutes = (_seconds % 3600) ~/ 60;
    int secs = _seconds % 60;

    return '${hours.toString().padLeft(2, '0')}:${minutes.toString().padLeft(2, '0')}:${secs.toString().padLeft(2, '0')}';
  }

  // 获取当前秒数
  int get seconds => _seconds;

  // 获取运行状态
  bool get isRunning => _isRunning;

  // 开始计时
  void start() {
    if (!_isRunning) {
      _isRunning = true;
      _timer = Timer.periodic(Duration(seconds: 1), (timer) {
        _seconds++;
        print('当前时间: $timeString');
      });
      print('计时器已开始');
    }
  }

  // 暂停计时
  void pause() {
    if (_isRunning) {
      _isRunning = false;
      _timer?.cancel();
      print('计时器已暂停，当前时间: $timeString');
    }
  }

  // 重置计时器
  void reset() {
    _isRunning = false;
    _timer?.cancel();
    _seconds = 0;
    print('计时器已重置');
  }

  // 停止计时器
  void stop() {
    _isRunning = false;
    _timer?.cancel();
    print('计时器已停止，最终时间: $timeString');
  }
}

void main() {
  print('=== Dart 计时器演示 ===');

  // 创建计时器实例
  MyTimer timer = MyTimer();

  // 演示计时器功能
  print('1. 开始计时');
  timer.start();

  // 模拟运行3秒后暂停
  Future.delayed(Duration(seconds: 3), () {
    print('\n2. 暂停计时');
    timer.pause();

    // 2秒后继续计时
    Future.delayed(Duration(seconds: 2), () {
      print('\n3. 继续计时');
      timer.start();

      // 再运行2秒后停止
      Future.delayed(Duration(seconds: 2), () {
        print('\n4. 停止计时');
        timer.stop();

        // 重置计时器
        Future.delayed(Duration(seconds: 1), () {
          print('\n5. 重置计时器');
          timer.reset();

          // 重新开始计时
          Future.delayed(Duration(seconds: 1), () {
            print('\n6. 重新开始计时');
            timer.start();

            // 运行1秒后结束演示
            Future.delayed(Duration(seconds: 1), () {
              timer.stop();
              print('\n=== 计时器演示结束 ===');
            });
          });
        });
      });
    });
  });
}

// 简单的倒计时器示例
class CountdownTimer {
  Timer? _timer;
  int _remainingSeconds;
  final int _totalSeconds;
  bool _isRunning = false;

  CountdownTimer(this._totalSeconds) : _remainingSeconds = _totalSeconds;

  String get timeString {
    int hours = _remainingSeconds ~/ 3600;
    int minutes = (_remainingSeconds % 3600) ~/ 60;
    int secs = _remainingSeconds % 60;

    return '${hours.toString().padLeft(2, '0')}:${minutes.toString().padLeft(2, '0')}:${secs.toString().padLeft(2, '0')}';
  }

  bool get isRunning => _isRunning;
  bool get isFinished => _remainingSeconds <= 0;

  void start() {
    if (!_isRunning && _remainingSeconds > 0) {
      _isRunning = true;
      _timer = Timer.periodic(Duration(seconds: 1), (timer) {
        _remainingSeconds--;
        print('倒计时: $timeString');

        if (_remainingSeconds <= 0) {
          _isRunning = false;
          timer.cancel();
          print('倒计时结束！');
        }
      });
    }
  }

  void pause() {
    if (_isRunning) {
      _isRunning = false;
      _timer?.cancel();
    }
  }

  void reset() {
    _isRunning = false;
    _timer?.cancel();
    _remainingSeconds = _totalSeconds;
  }
}

// 倒计时器演示
void countdownDemo() {
  print('\n=== 倒计时器演示 ===');
  CountdownTimer countdown = CountdownTimer(5); // 5秒倒计时

  print('开始5秒倒计时');
  countdown.start();
}
